Translation PRO
The Translation API enables text translation between different languages. It supports both individual and batch translation use cases and is available on iOS 18.0 or later.
Overview
The API is exposed as a Translation class, which includes:
- A shared singleton instance for global use
- Methods for translating single or multiple strings
- Support for automatic language detection based on user preferences
Class: Translation
Translation.shared: Translation
Provides a shared singleton instance of the Translation class. This is useful in scripts that do not have a UI or need to reuse a common translation host.
Example
Method: translate(options): Promise<string>
Translates a single string from a source language to a target language.
Parameters
-
options.text: stringThe input string to be translated. -
options.source?: stringThe source language code (e.g.,"en"for English). If omitted ornull, the translation system will attempt to detect the source language automatically. If detection is ambiguous, the user may be prompted to clarify. -
options.target?: stringThe target language code (e.g.,"es"for Spanish). If omitted ornull, the system will choose an appropriate target language based on the device’sDevice.preferredLanguagesand the detected source language.
Returns
Promise<string>— A promise that resolves to the translated string.
Throws
- An error if the translation fails (e.g., network issues, unsupported languages, etc.).
Example
Method: translateBatch(options): Promise<string[]>
Translates an array of strings from a source language to a target language.
Parameters
-
options.texts: string[]An array of strings to translate. The order of translations in the result matches the input array. -
options.source?: stringThe source language code. Behaves the same as in thetranslatemethod. -
options.target?: stringThe target language code. Behaves the same as in thetranslatemethod.
Returns
Promise<string[]>— A promise that resolves to an array of translated strings.
Throws
- An error if any part of the batch translation fails.
Example
Notes
-
Language codes should follow ISO 639-1 format (e.g.,
"en","zh","de","fr"). -
This API leverages system-level translation capabilities and may prompt for language disambiguation in certain cases.
-
Use the
translationHostview modifier in the following scenarios:-
Interactive Translations in UI Views When your script presents a user interface (e.g., using
<VStack>,<List>, etc.) and performs translations using a customTranslationinstance (created vianew Translation()), you must applytranslationHostto the root view to allow the system to display permission dialogs, language download prompts, or source language selection alerts. -
Source Language Is
nullIf you omit thesourcefield in your translation request and rely on the system to detect the language,translationHostensures the system can prompt the user if detection fails. -
Languages May Need Downloading If the device does not have the required source or target language installed,
translationHostenables system prompts to download those languages interactively.
-
-
You do not need to set
translationHostwhen using the pre-boundTranslation.sharedinstance in a headless or background script that does not render a user interface.
